
package com.mephi.gui;


import java.awt.Color;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.apache.log4j.Logger;
import com.mephi.model.GameMovesModel;
import com.mephi.util.AuxClass;


/**
 * A default implementation of chess score sheet with visualization of moves
 * in the standard chess notation.
 * 
 * @author mephisto
 * 2011-07-23
 */
public class ChessScoreSheet extends JPanel
{

	public ChessScoreSheet(GameMovesModel game) 
	{
		super();
		setGameModel(game);
		setBackground(Color.LIGHT_GRAY);
		setLayout(new GridBagLayout());
		try
		{
			FileInputStream fis = new FileInputStream(
					new File("").getAbsolutePath()+
					"/bin/com/mephi/resources/LabelsBundle");
			labelsBundle = new PropertyResourceBundle(fis);
		}
		catch (FileNotFoundException e)
		{
			logger.error(e);
		}
		catch (IOException e)
		{
			logger.error(e);
		}
		create();
	}
    
	/**
	 * @return the gameModel
	 */
	public GameMovesModel getGameModel()
	{
		return gameModel;
	}

	/**
	 * @param gameModel the gameModel to set
	 * @throws NullPointerException if dameModel is null
	 */
	public void setGameModel(GameMovesModel gameModel)
	{
		AuxClass.checkArgument(gameModel, new NullPointerException("Gamemodel cannot be null!"));
		this.gameModel = gameModel;
		this.gameModel.fireTableStructureChanged();
	}
	
	/**
	 * Updates model using gameModel and repaints chess score sheet
	 * retrieving data from gameModel.
	 * 
	 * @param gameModel the new gameModel to set
	 */
	public void update(GameMovesModel gameModel)
	{
		setGameModel(gameModel);
		update();
	}
	
	/**
	 * Repaints chess score sheet retrieving data from gameModel.
	 */
	public void update()
	{
		lblEvent.setText(labelsBundle.getString(
				"eventKey") + gameModel.getGameModel().getEvent());
		lblSite.setText(labelsBundle.getString(
				"siteKey") + gameModel.getGameModel().getSite());
		StringBuilder date = new StringBuilder(
				gameModel.getGameModel().getCalendar().getTime().toString());
		lblCalendar.setText(labelsBundle.getString("calendarKey")+ date);
		lblRound.setText(labelsBundle.getString(
				"roundKey") + gameModel.getGameModel().getRound());
		lblWhite.setText(labelsBundle.getString(
				"whiteKey") + gameModel.getGameModel().getWhite());
		lblBlack.setText(labelsBundle.getString(
				"blackKey") + gameModel.getGameModel().getBlack());
		lblResult.setText(labelsBundle.getString(
				"resultKey") + gameModel.getGameModel().getResult());
		lblTimeControl.setText(labelsBundle.getString(
				"timeControlKey") + gameModel.getGameModel().getTimeControl());
		lblWhiteElo.setText(labelsBundle.getString(
				"whiteEloKey") + gameModel.getGameModel().getWhiteElo());
		lblBlackElo.setText(labelsBundle.getString(
				"blackEloKey") + gameModel.getGameModel().getBlackElo());
		
		GameMovesModel dataModel = new GameMovesModel(gameModel.getGameModel());
		tabMoves.setModel(dataModel);
		repaint();
	}
	
	private void create()
	{
		add(getLblEvent(), 
				new GBC(0,0).setFill(GBC.HORIZONTAL).setInsets(5,10,5,10));
		add(getLblSite(), 
				new GBC(0,1).setFill(GBC.HORIZONTAL).setInsets(5,10,5,10));
		add(getLblCalendar(), 
				new GBC(1,0,3,1).setFill(GBC.VERTICAL).setInsets(5,10,5,10));
		add(getLblRound(), 
				new GBC(1,1).setFill(GBC.VERTICAL).setInsets(5));
		add(getLblResult(), 
				new GBC(2,1).setFill(GBC.VERTICAL).setInsets(5));
		add(getLblTimeControl(), 
				new GBC(3,1).setFill(GBC.VERTICAL).setInsets(5));
		
		add(getLblWhite(), 
				new GBC(0,2).setFill(GBC.VERTICAL).setInsets(5,10,5,10));
		add(getLblWhiteElo(), 
				new GBC(1,2).setFill(GBC.VERTICAL).setInsets(5,10,5,10));
			
		add(getLblBlack(), 
				new GBC(0,3).setFill(GBC.VERTICAL).setInsets(5,10,5,10));
		add(getLblBlackElo(), 
				new GBC(1,3).setFill(GBC.VERTICAL).setInsets(5,10,5,10));
		JScrollPane tabScrollPane = new JScrollPane(getTabMoves());
		add(tabScrollPane, 
				new GBC(0,4,4,3).setFill(GBC.VERTICAL).setInsets(5,10,5,10));
	}

	/**
	 * @return the lblEvent
	 */
	private JLabel getLblEvent()
	{
		lblEvent = new JLabel(labelsBundle.getString(
				"eventKey") + gameModel.getGameModel().getEvent());
		return lblEvent;
	}

	/**
	 * @return the lblSite
	 */
	private JLabel getLblSite()
	{
		lblSite = new JLabel(labelsBundle.getString(
				"siteKey")+ gameModel.getGameModel().getSite());
		return lblSite;
	}

	/**
	 * @return the lblCalendar
	 */
	private JLabel getLblCalendar()
	{
		StringBuilder date = new StringBuilder(
				gameModel.getGameModel().getCalendar().getTime().toString());
		lblCalendar = new JLabel(labelsBundle.getString("calendarKey")+ date);
		return lblCalendar;
	}

	/**
	 * @return the lblRound
	 */
	private JLabel getLblRound()
	{
		lblRound = new JLabel(labelsBundle.getString(
				"roundKey")+ gameModel.getGameModel().getRound());
		return lblRound;
	}

	/**
	 * @return the lblWhite
	 */
	private JLabel getLblWhite()
	{
		lblWhite = new JLabel(labelsBundle.getString(
				"whiteKey")+ gameModel.getGameModel().getWhite());
		return lblWhite;
	}

	/**
	 * @return the lblBlack
	 */
	private JLabel getLblBlack()
	{
		lblBlack = new JLabel(labelsBundle.getString(
				"blackKey")+ gameModel.getGameModel().getBlack());
		return lblBlack;
	}
	
	/**
	 * @return the lblResult
	 */
	private JLabel getLblResult()
	{
		lblResult = new JLabel(labelsBundle.getString(
				"resultKey")+ gameModel.getGameModel().getResult());
		return lblResult;
	}

	/**
	 * @return the lblTimeControl
	 */
	private JLabel getLblTimeControl()
	{
		lblTimeControl = new JLabel(labelsBundle.getString(
				"timeControlKey")+ gameModel.getGameModel().getTimeControl());
		return lblTimeControl;
	}

	/**
	 * @return the lblWhiteElo
	 */
	private JLabel getLblWhiteElo()
	{
		lblWhiteElo = new JLabel(labelsBundle.getString(
			"whiteEloKey")+ gameModel.getGameModel().getWhiteElo());
		return lblWhiteElo;
	}

	/**
	 * @return the lblBlackElo
	 */
	private JLabel getLblBlackElo()
	{
		lblBlackElo = new JLabel(labelsBundle.getString(
			"blackEloKey")+ gameModel.getGameModel().getBlackElo());
		return lblBlackElo;
	}

	/**
	 * @return the tabMoves
	 */
	private JTable getTabMoves()
	{	
		GameMovesModel model = new GameMovesModel(gameModel.getGameModel());
		tabMoves = new JTable(model);	
		tabMoves.setDefaultRenderer(String.class, new MoveDefaultRenderer());
		return tabMoves;
	}

	   
	private static final long serialVersionUID = 4945204963188142805L;
	private JLabel lblEvent; 
	private JLabel lblSite;
	private JLabel lblCalendar;
	private JLabel lblRound;
	private JLabel lblWhite;
	private JLabel lblResult;
	private JLabel lblBlack;
	private JLabel lblTimeControl;
	private JLabel lblWhiteElo;
	private JLabel lblBlackElo;
	private JTable tabMoves;
	private GameMovesModel gameModel;
	
	//mapa etykiet wspieranie wielu jezykow
	private ResourceBundle labelsBundle; 
	private Logger logger = Logger.getLogger("LOG");
}
